﻿using System;
using System.Windows;
using System.Runtime.InteropServices.Automation;

using SBPweb.RIA.Client.Common.Patterns;

using SBPweb.PROvibe.Client.Library.Controls;
using SBPweb.PROvibe.Client.Library.Models;

namespace SBPweb.PROvibe.Client.Library.Base
{

    public class ShellContext : IShellContextModel
    {

        #region Fields

        private static SingletonHelper<ShellContext> sh = new SingletonHelper<ShellContext>();

        #endregion

        #region Properties

        public static IShellContextModel SingletonInstance
        {
            get
            {
                return sh.Instance;
            }
        }

        public bool IsAvailable
        {
            get
            {
                return true;
            }
        }

        #endregion

        #region Methods

        public void ExecuteCommand(string command)
        {
            if (Application.Current.IsRunningOutOfBrowser)
            {
                using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
                {
                    try
                    {
                        shell.Run(command);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
            else
            {
                HyperlinkButtonMocker hlbm = new HyperlinkButtonMocker();
                hlbm.RaiseClick(new Uri(string.Format("file:///{0}", command), UriKind.RelativeOrAbsolute));
            }
        }

        #endregion

    }

}